package cn.com.huzo.service.manufacture.impl;

import cn.com.huzo.model.entities.manufacture.CommissionProcessingContractLinesMoEntity;
import cn.com.huzo.repository.manufacture.CommissionProcessingContractHeaderRepository;
import cn.com.huzo.repository.manufacture.CommissionProcessingContractLinesMoRepository;
import cn.com.huzo.repository.manufacture.CommissionProcessingContractLinesRepository;
import cn.com.huzo.service.manufacture.CommissionProcessingContractLinesMoManager;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 加工承揽合同-加工明细 管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("CommissionProcessingContractLinesMoManager")
public class CommissionProcessingContractLinesMoManagerImpl extends GenericManagerImpl<CommissionProcessingContractLinesMoEntity, String>
        implements CommissionProcessingContractLinesMoManager {

    @Resource(name = "CommissionProcessingContractHeaderRepository")
    private CommissionProcessingContractHeaderRepository _dao;

    @Resource(name = "CommissionProcessingContractLinesMoRepository")
    private CommissionProcessingContractLinesMoRepository _dao_lines_mo;

    @Resource(name = "CommissionProcessingContractLinesRepository")
    private CommissionProcessingContractLinesRepository _dao_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<CommissionProcessingContractLinesMoEntity> fetchByHeaderKey(Long headerKey) throws Exception {
        return _dao_lines_mo.fetchByHeaderKey(headerKey);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchInsert(List<CommissionProcessingContractLinesMoEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();

        // step 1： 根据选定的生产单数据，插入加工明细
        Integer rowsAffected = _dao_lines_mo.batchInsert(itemList);
        // step 2: 取得第一步插入后的加工明细按款号与现有的合同明细款号之间的差集，然后汇总插入合同明细信息
        _dao_lines.insert(itemList.get(0).getHeaderId(), itemList.get(0).getCreatedBy(), itemList.get(0).getUpdatedBy());
        // step 3: 同步加工明细中相同款号的数量（保持二者数据的一致性，避免因为插入或者删除导致问题）
        _dao_lines.updateForSyncQuantity(itemList.get(0).getHeaderId(), itemList.get(0).getUpdatedBy());

        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(itemList.get(0).getUpdatedBy(), "生产管理→委托加工合同加工明细",
                    "合同号：" + itemList.get(0).getContractNo(),
                    "新增");
        }

        _result.put("Amount", _dao.fetchAmountByKey(itemList.get(0).getHeaderId()));
        _result.put("LinesMoList", _dao_lines_mo.fetchByHeaderKey(itemList.get(0).getHeaderId()));
        _result.put("LinesList", _dao_lines.fetchByHeaderKey(itemList.get(0).getHeaderId()));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(List<CommissionProcessingContractLinesMoEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();
        if (itemList.isEmpty()) {
            return _result;
        }

        // step 1： 更新加工明细记录
        Integer rowsAffected = _dao_lines_mo.batchUpdate(itemList);
        // step 2: 更新合同明细中和加工明细中相同款号的数量（保持二者数据的一致性，避免因为插入或者删除导致问题）
        _dao_lines.updateForSyncQuantity(itemList.get(0).getHeaderId(), itemList.get(0).getUpdatedBy());

        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(itemList.get(0).getUpdatedBy(), "生产管理→委托加工合同加工明细",
                    "合同号：" + itemList.get(0).getContractNo(),
                    "更新");
        }

        _result.put("Amount", _dao.fetchAmountByKey(itemList.get(0).getHeaderId()));
        _result.put("LinesMoList", _dao_lines_mo.fetchByHeaderKey(itemList.get(0).getHeaderId()));
        _result.put("LinesList", _dao_lines.fetchByHeaderKey(itemList.get(0).getHeaderId()));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> deleteLines(CommissionProcessingContractLinesMoEntity instance) throws Exception {
        Map<String, Object> _result = new HashMap();

        // step 1： 删除加工明细记录
        Integer rowsAffected = _dao_lines_mo.delete(instance.getLinesId());
        // step 2： 删除合同明细记录
        _dao_lines.deleteForSyncLinesMo(instance.getHeaderId());
        // step 3: 更新合同明细中和加工明细中相同款号的数量（保持二者数据的一致性，避免因为插入或者删除导致问题）
        _dao_lines.updateForSyncQuantity(instance.getHeaderId(), instance.getUpdatedBy());

        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(instance.getDeletedBy(), "生产管理→委托加工合同加工明细",
                    "合同号：" + instance.getContractNo() + "　款号:" + instance.getStyleNo()
                    + "　颜色:" + instance.getColourName() + "　尺码:" + instance.getSizeName(),
                    "删除");
        }

        _result.put("Amount", _dao.fetchAmountByKey(instance.getHeaderId()));
        _result.put("LinesList", _dao_lines.fetchByHeaderKey(instance.getHeaderId()));
        _result.put("LinesMoList", _dao_lines_mo.fetchByHeaderKey(instance.getHeaderId()));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchDelete(List<CommissionProcessingContractLinesMoEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();

        // step 1： 删除加工明细记录
        Integer rowsAffected = _dao_lines_mo.batchDelete(itemList);
        // step 2： 删除合同明细记录
        _dao_lines.deleteForSyncLinesMo(itemList.get(0).getHeaderId());
        // step 3: 同步加工明细中加工数量
        _dao_lines.updateForSyncQuantity(itemList.get(0).getHeaderId(), itemList.get(0).getUpdatedBy());

        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(itemList.get(0).getDeletedBy(), "生产管理→委托加工合同加工明细",
                    "合同号：" + itemList.get(0).getContractNo(),
                    "批量删除");
        }
        
        _result.put("Amount", _dao.fetchAmountByKey(itemList.get(0).getHeaderId()));
        _result.put("LinesList", _dao_lines.fetchByHeaderKey(itemList.get(0).getHeaderId()));
        _result.put("LinesMoList", _dao_lines_mo.fetchByHeaderKey(itemList.get(0).getHeaderId()));

        return _result;
    }
}
